<?php
include '../conn.php';
$data = json_decode($HTTP_RAW_POST_DATA);
$sql = "select * from users_info
        where id = $data->userId
        limit 1";
$isUser = $pdo->getOne($sql);
if($isUser) {
    $imgs = "";
    foreach($data->images as $img):
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img, $result)){
            $type = $result[2];
            $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
            $new_file = "../images/news/$image_name";
            $img_url = substr($new_file,2);
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img)))){
                $imgs .= "$img_url,";
            };
        };
    endforeach;
    $imgstr = rtrim($imgs,",");
    $sql = "update users_content set
            user_id = $data->userId,
            title = '$data->title',
            images = '$imgstr',
            content = '$data->content',
            type = $data->type,
            status = $data->status,
            update_time = '$data->time'
            where id = $data->id";
    $req = $pdo->query($sql);
}else {
    $arr = [
        "code" => 400,
        "desc" => '用户信息错误,请重试'
    ];
    echo json_encode($arr);
    exit;
}